package ytdp.careagle.com.ytdp.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wx.wheelview.widget.WheelView;

import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.activity.BaseActivity;
import ytdp.careagle.com.ytdp.adapter.MyCityAdapter;
import ytdp.careagle.com.ytdp.adapter.MyProvinceAdapter;
import ytdp.careagle.com.ytdp.entity.ProvinceEntity;

public class SelectProvinceDialog extends BaseActivity implements WheelView.OnWheelItemSelectedListener {

    @Bind(R.id.province_wheelview)
    WheelView provinceWheelview;
    @Bind(R.id.city_wheelview)
    WheelView cityWheelview;
    @Bind(R.id.tv_cancel)
    TextView tvCancel;
    @Bind(R.id.tv_ok)
    TextView tvOk;
    @Bind(R.id.ll_gen)
    RelativeLayout llGen;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.ll_viewGroup)
    LinearLayout llViewGroup;
    private int selectProvinceIndex = 0;
    private int selectCityIndex = 0;
    private boolean hasCityAndIsFirst = false;
    private List<ProvinceEntity> provinceList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_province_dialog;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_ok, R.id.ll_gen})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_gen:
                finish();
                break;
            case R.id.tv_ok:
                selectOk();
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_down);
    }

    private void selectOk() {
        ProvinceEntity provinceEntity = (ProvinceEntity) provinceWheelview.getSelectionItem();
        ProvinceEntity.ChildrenBean childrenBean = (ProvinceEntity.ChildrenBean) cityWheelview.getSelectionItem();
        Intent intent = new Intent();
        intent.putExtra("provinceName", provinceEntity.getProvinceName());
        intent.putExtra("provinceCode", provinceEntity.getProvinceCode());
        intent.putExtra("cityName", childrenBean.getCityName());
        intent.putExtra("cityCode", childrenBean.getCityCode());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        String provinceName = intent.getStringExtra("provinceName");
        String cityName = intent.getStringExtra("cityName");
        int bottom = intent.getIntExtra("bottom", -1);
        bottom += getResources().getDimension(R.dimen.dp15);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) llViewGroup.getLayoutParams();
        layoutParams.setMargins(0, bottom, 0, 0);
        provinceList = (List<ProvinceEntity>) intent.getSerializableExtra("data");
        if (!TextUtils.isEmpty(provinceName)) {
            for (int i = 0; i < provinceList.size(); i++) {
                if (provinceList.get(i).getProvinceName().equals(provinceName)) {
                    selectProvinceIndex = i;
                    break;
                }
            }
        }
        if (!TextUtils.isEmpty(cityName)) {
            List<ProvinceEntity.ChildrenBean> children = provinceList.get(selectProvinceIndex).getChildren();
            for (int i = 0; i < children.size(); i++) {
                if (cityName.equals(children.get(i).getCityName())) {
                    selectCityIndex = i;
                    hasCityAndIsFirst = true;
                    break;
                }
            }
        }
        provinceWheelview.setWheelAdapter(new MyProvinceAdapter(this));
        provinceWheelview.setWheelSize(5);
        provinceWheelview.setSkin(WheelView.Skin.Holo);
        provinceWheelview.setWheelData(provinceList);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextSize = 30;
        style.textSize = 24;
        provinceWheelview.setStyle(style);

        cityWheelview.setWheelAdapter(new MyCityAdapter(this));
        cityWheelview.setWheelSize(5);
        cityWheelview.setSkin(WheelView.Skin.Holo);
        List<ProvinceEntity.ChildrenBean> childrens = provinceList.get(selectProvinceIndex).getChildren();
        cityWheelview.setWheelData(childrens);
        cityWheelview.setStyle(style);
//        provinceWheelview.join(cityWheelview);
//        HashMap<ProvinceEntity, List<ProvinceEntity.ChildrenBean>> map = new HashMap<>();
//        for (ProvinceEntity provinceEntity : provinceList) {
//            map.put(provinceEntity, provinceEntity.getChildren());
//        }
//        provinceWheelview.joinDatas(map);
        provinceWheelview.setOnWheelItemSelectedListener(this);
        if (selectProvinceIndex != 0) {
            provinceWheelview.setSelection(selectProvinceIndex);
        }
        if (selectCityIndex != 0) {
            cityWheelview.setSelection(selectCityIndex);
        }
    }

    @Override
    public void onItemSelected(int position, Object o) {
        if (hasCityAndIsFirst) {
            hasCityAndIsFirst = !hasCityAndIsFirst;
        } else {
            cityWheelview.setWheelData(provinceList.get(position).getChildren());
        }
    }
}
